<?php

namespace App\Http\Controllers;

class BucketController extends OssClientController
{
    public function store($bucket)
    {
        try {
            $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PRIVATE);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        print(__FUNCTION__ . ": OK" . "\n");
    }

    public function exist($bucket)
    {
        try {
            $res = $ossClient->doesBucketExist('zhurongpeng');
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        if ($res === true) {
            print(__FUNCTION__ . ": OK" . "\n");
        } else {
            print(__FUNCTION__ . ": FAILED" . "\n");
        }
    }

    public function index($ossClient)
    {
        $bucketList = null;

        try{
            $bucketListInfo = $ossClient->listBuckets();
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        $bucketList = $bucketListInfo->getBucketList();

        foreach($bucketList as $bucket) {
            print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
        }
    }

    public function destory($ossClient, $bucket)
    {
        try{
            $ossClient->deleteBucket($bucket);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        print(__FUNCTION__ . ": OK" . "\n");
    }

    public function putBucketAcl($ossClient, $bucket)
    {
        $acl = OssClient::OSS_ACL_TYPE_PRIVATE;

        try {
            $ossClient->putBucketAcl($bucket, $acl);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        print(__FUNCTION__ . ": OK" . "\n");
    }

    public function getBucketAcl($ossClient, $bucket)
    {
        try {
            $res = $ossClient->getBucketAcl($bucket);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        print(__FUNCTION__ . ": OK" . "\n");
        print('acl: ' . $res);
    }

}
